package top.lhj.security.example.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Component;
import top.lhj.security.example.entity.UserEntity;
import top.lhj.security.example.mapper.UserMapper;

import java.lang.reflect.GenericArrayType;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @Author LiHaoJie
 * @Create 2024/2/3 13:59
 * @Version 1.0
 */
@Component
public class DbUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {
    
    @Autowired
    UserMapper userMapper;
    
    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return null;
    }
    
    @Override
    public void createUser(UserDetails user) {
        UserEntity userEntity = new UserEntity()
                .setUsername(user.getUsername())
                .setPassword(user.getPassword())
                .setEnabled(1);
        int i = userMapper.insert(userEntity);
        if (i == 0) {
            throw new RuntimeException("用户保存失败");
        }
    }
    
    @Override
    public void updateUser(UserDetails user) {
        
    }
    
    @Override
    public void deleteUser(String username) {
        
    }
    
    @Override
    public void changePassword(String oldPassword, String newPassword) {
        
    }
    
    @Override
    public boolean userExists(String username) {
        return false;
    }
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserEntity userEntity = getUserEntityByUsername(username);
        if (userEntity == null) {
            throw new UsernameNotFoundException(username);
        } else {
            Collection<GrantedAuthority> authorities = new ArrayList<>();
            //TODO 这里用户一般有一个权限字段，或者单独设计一个用户权限表，可以是一对多或者多对多的关系
//            authorities.add(() -> "USER_ADD");
//            authorities.add(() -> "USER_LIST");
           /* return new User(
                    userEntity.getUsername(),
                    userEntity.getPassword(),
                    userEntity.getEnabled() == 1 ? true : false,
                    true,   //用户账号是否过期
                    true,           //用户凭证是否过期
                    true,           //用户是否未被锁定
                    authorities     //权限列表
            );*/
            return User.withUsername(userEntity.getUsername())
                    .password(userEntity.getPassword())
                    .roles("ADMIN")
                    .authorities("USER_ADD", "USER_UPDATE")
                    .credentialsExpired(!(userEntity.getEnabled() == 1 ? true : false))
                    .accountExpired(false)
                    .accountLocked(false)
                    .build();
        }
    }
    
    public UserEntity getUserEntityByUsername(String username) {
        return userMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, username));
    }
}
